<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="跳转链接" prop="hrefUrl">
        <el-input
            v-model="queryParams.hrefUrl"
            placeholder="请输入跳转链接"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="轮播图类型" prop="type" label-width="90">
        <el-select v-model="queryParams.type" clearable placeholder="请选择轮播图类型" style="width: 200px">
          <el-option
              v-for="dict in banner_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            v-hasPermi="['media:banner:add']"
            icon="Plus"
            @click="handleAdd"
            plain
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['media:banner:edit']"
            icon="Edit"
            plain
            :disabled="single"
            @click="handleUpdate"
            type="success"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['media:banner:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            @click="handleDelete"
            type="danger"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['media:banner:export']"
            plain
            icon="Download"
            @click="handleExport"
            type="warning"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="轮播图ID" prop="id"/>
      <el-table-column align="center" label="跳转链接" prop="hrefUrl"/>
      <el-table-column align="center" label="图片地址" prop="imgUrl" width="100">
        <template #default="scope">
          <image-preview :height="50" :src="scope.row.imgUrl" :width="50"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="轮播图类型" prop="type">
        <template #default="scope">
          <dict-tag :options="banner_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="status">
        <template #default="scope">
          <dict-tag :options="banner_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建者" prop="createBy"/>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新者" prop="updateBy"/>
      <el-table-column align="center" label="更新时间" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="remark"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" fixed="right">
        <template #default="scope">
          <el-button v-hasPermi="['media:banner:edit']" icon="Edit" link type="primary"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-hasPermi="['media:banner:remove']" icon="Delete" link type="primary"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改轮播图对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="500px" >
      <el-form ref="bannerRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="跳转链接" prop="hrefUrl">
          <el-input v-model="form.hrefUrl" placeholder="请输入跳转链接"/>
        </el-form-item>
        <el-form-item label="上传方式" prop="uploadMethod">
          <el-radio-group v-model="form.uploadMethod">
            <el-radio v-for="dict in picture_upload_method" :key="dict.value"
                      :label="dict.value">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图片地址" prop="imgUrl">
          <div v-if="form.uploadMethod==='1'">
            <my-img-upload v-model="form.imgUrl" limit="1"/>
            <!--          <image-upload v-model="form.imgUrl"/>-->
            <div v-if="form.imgUrl!==null">
              {{ form.imgUrl }}
            </div>
          </div>
          <div v-else>
            <el-input v-model="form.imgUrl" placeholder="请输入图片地址" />
          </div>
        </el-form-item>
        <el-form-item label="轮播图类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择轮播图类型">
            <el-option
                v-for="dict in banner_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Banner" setup>
import {addBanner, delBanner, getBanner, listBanner, updateBanner} from "@/api/media/banner";
import MyImgUpload from "@/components/MyImgUpload/index.vue";

const {proxy} = getCurrentInstance();
const {banner_type, picture_upload_method,banner_status} = proxy.useDict('banner_type', 'picture_upload_method','banner_status');

const bannerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hrefUrl: null,
    imgUrl: null,
    type: null,
    status: null,
  },
  rules: {
    type: [
      {required: true, message: "轮播图类型不能为空", trigger: "change"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询轮播图列表 */
function getList() {
  loading.value = true;
  listBanner(queryParams.value).then(response => {
    bannerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    hrefUrl: null,
    imgUrl: null,
    type: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("bannerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加轮播图";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBanner(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改轮播图";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bannerRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBanner(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBanner(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除轮播图编号为"' + _ids + '"的数据项？').then(function () {
    return delBanner(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('media/banner/export', {
    ...queryParams.value
  }, `banner_${new Date().getTime()}.xlsx`)
}

getList();
</script>
